var app = getApp();
Page({
    data:{
        curPage : "/zofui_sales/jifen/order",
        page : {
            isend : false,
            doing : false,
            doo : 'jifen',
            pdata : {
                op : 'orderlist',
                page : 1,
                status : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        tabMenu :  [{
            name: "全部",
            status: 0
        }, {
            name: "待支付",
            status: 4
        }, {
            name: "待发货",
            status: 1
        }, {
            name: "已发货",
            status: 2
        }, {
            name: "已完成",
            status: 3
        }],
        Menu: [{
            url: "/zofui_sales/pages/index/index",
            iconPath: "/images/menu01.png",
            selectedIconPath: "/images/menu01.png",
            title: "首页",
            type: "tabbar"
        }, {
            url: "/zofui_sales/jifen/index",
            iconPath: "/images/jf_a.png",
            selectedIconPath: "/images/jf_a1.png",
            title: "兑换"
        }, {
            url: "/zofui_sales/jifen/order",
            iconPath: "/images/menu04.png",
            selectedIconPath: "/images/menu04-on.png",
            title: "订单"
        }],
        isIPX : app.globalData.isIPX,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        var zfid = options.zfid ? options.zfid : '';
        self.setData({
            'page.pdata.status' : options.status ? options.status : 0,
            'page.pdata.zfid' : zfid,
        });

        app.com.auth(function(user){
            app.com.getPage(self,self.data.page,20,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   Pagetheme : res.data.data.params.style,
				   params : res.data.data.params,
                   sharedata : res.data.data.sharedata,
                },function(){
                    self.setData({
                        inited : true,
                    });
                });

            },false,function(res){
                app.com.alert(res.data.message);
            });
        });

        wx.setNavigationBarTitle({
          title:  '我的兑换',
        });
    },
    onShow(){
        var self = this;
        if( this.data.reload == 1 ){
            self.onLoad(self.data.options);  
            self.setData({
                reload : 0,
            });
        }
    },
    tabChange(e){
        var self = this;

        if( self.data.ising || ( e.target.dataset.status < 3 && e.target.dataset.status == self.data.page.pdata.status ) ) return false;
        self.data.ising = true;

        self.setData({
            'page.pdata.status' : e.currentTarget.dataset.status,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
            wx.pageScrollTo({
                scrollTop: 0
            });
        });

        self.data.ising = false;
    },
    payit : function(e){
        var postdata = {
            tid : e.currentTarget.dataset.id,
            op : 'repay'
        }
        app.com.http('jifen','POST',postdata,0,true,'','',function(res){
            if ( !res.data.errno && res.data.errno !== undefined ) {
                wx.requestPayment({
					'appId' : res.data.data.appId,
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success': function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            for (var i = 0; i < self.data.datalist.length; i++) {
                                if( self.data.datalist[i].id == postdata.tid ){
                                    self.setData({
                                        ['datalist['+i+'].status'] : 0,
                                    })
                                }
                            }
                        }
                    },
                    'fail': function (payres) {
                        console.log(payres);
                    }
                });
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    como(e){
        var self = this;
        var postdata = {
            tid : e.currentTarget.dataset.id,
            op : 'comorder'
        }
        app.com.confirm('确定完成订单吗？',function(){
            app.com.http('jifen','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    for (var i = 0; i < self.data.datalist.length; i++) {
                        if( self.data.datalist[i].id == postdata.tid ){
                            self.setData({
                                ['datalist['+i+'].status'] : 2,
                            })
                        }
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        })
    },
    express(e){
        var name = e.currentTarget.dataset.name;
        var num = e.currentTarget.dataset.num;
        app.com.alert('名称:'+name+'，编号:'+num);
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },

})